/*
 * This file is part of gshot.
 *
 * Gshot is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Gshot is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Gshot; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright (C) 2007-2011, Victor Ananjevsky <ananasik@gmail.com>
 */

#include <stdlib.h>

#include "gshot.h"

shot_t data;

int read_settings ()
{
  gchar *cfg_file;
  GKeyFile *cfg;
  gchar **keys, **keys_p;

  /* Set default values */
  data.frame = TRUE;
  data.hide = TRUE;
  data.mode = GRAB_SCREEN;
  data.delay = 0;
  data.image = NULL;
  data.folder = strdup (getenv ("HOME"));
  data.tray = TRUE;
  data.tray_hide = TRUE;

  data.print = NULL;
  data.page = NULL;

  cfg_file = g_build_filename (g_get_user_config_dir (), "gshotrc", NULL);
  cfg = g_key_file_new ();

  /* Parse config file */
  if (g_key_file_load_from_file (cfg, cfg_file, G_KEY_FILE_NONE, NULL) )
    {
      if (g_key_file_has_group (cfg, "Common"))
	{
          if (g_key_file_has_key (cfg, "Common", "Frame", NULL))
            data.frame = g_key_file_get_boolean (cfg, "Common", "Frame", NULL);
          if (g_key_file_has_key (cfg, "Common", "Hide", NULL))
            data.frame = g_key_file_get_boolean (cfg, "Common", "Hide", NULL);
          if (g_key_file_has_key (cfg, "Common", "Mode", NULL))
            data.mode = g_key_file_get_integer (cfg, "Common", "Mode", NULL);
          if (g_key_file_has_key (cfg, "Common", "Delay", NULL))
            data.delay = g_key_file_get_integer (cfg, "Common", "Delay", NULL);
          if (g_key_file_has_key (cfg, "Common", "Folder", NULL))
	    {
	      g_free (data.folder);
	      data.folder =  g_key_file_get_string (cfg, "Common", "Folder", NULL);
	    }
          if (g_key_file_has_key (cfg, "Common", "Tray", NULL))
            data.tray = g_key_file_get_boolean (cfg, "Common", "Tray", NULL);
          if (g_key_file_has_key (cfg, "Common", "HideOnStartup", NULL))
            data.tray_hide = g_key_file_get_boolean (cfg, "Common", "HideOnStartup", NULL);
 	}

      /* get scripts */
      keys = keys_p = g_key_file_get_keys (cfg, "Scripts", NULL, NULL);
      if (keys)
	{
	  GtkListStore *model;
	  GtkTreeIter iter;

	  model = GTK_LIST_STORE (gtk_builder_get_object (data.xml, "scripts_store"));

	  while (*keys_p)
	    {
	      if (g_ascii_strncasecmp (*keys_p, "Name", 4) == 0)
		{
		  gint num;
		  gchar *buf, *name, *cmd;

		  sscanf (*keys_p, "Name%d", &num);
		  buf = g_strdup_printf ("Script%d", num);
		  name = g_key_file_get_string (cfg, "Scripts", *keys_p, NULL);
		  cmd = g_key_file_get_string (cfg, "Scripts", buf, NULL);

		  gtk_list_store_append (model, &iter);
		  gtk_list_store_set (model, &iter, 0, name, 1, cmd, -1);

		  g_free (name);
		  g_free (cmd);
		  g_free (buf);
		}
	      keys_p++;
	    }      
	  g_strfreev (keys);
	}

      /* get printer settings */
      data.print = gtk_print_settings_new_from_key_file (cfg, NULL, NULL);
      data.page = gtk_page_setup_new_from_key_file (cfg, NULL, NULL);
    }

  if (data.page == NULL)
    {
      GtkPaperSize *size = gtk_paper_size_new (GTK_PAPER_NAME_A4);

      data.page = gtk_page_setup_new ();

      gtk_page_setup_set_paper_size (data.page, size);
      gtk_page_setup_set_top_margin (data.page, 0.5, GTK_UNIT_INCH);
      gtk_page_setup_set_bottom_margin (data.page, 0.5, GTK_UNIT_INCH);
      gtk_page_setup_set_left_margin (data.page, 0.5, GTK_UNIT_INCH);
      gtk_page_setup_set_right_margin (data.page, 0.5, GTK_UNIT_INCH);
      gtk_page_setup_set_orientation (data.page, GTK_PAGE_ORIENTATION_LANDSCAPE);

      gtk_paper_size_free (size);
    }

  g_key_file_free (cfg);
  g_free (cfg_file);

  return 0;
}

int save_settings ()
{
  gchar *cfg_file;
  GKeyFile *cfg;
  GtkTreeModel *store;
  GtkTreeIter iter;

  cfg = g_key_file_new ();

  g_key_file_set_boolean (cfg, "Common", "Frame", data.frame);
  g_key_file_set_boolean (cfg, "Common", "Hide", data.hide);
  g_key_file_set_integer (cfg, "Common", "Mode", data.mode);
  g_key_file_set_integer (cfg, "Common", "Delay", data.delay);
  g_key_file_set_string (cfg, "Common", "Folder", data.folder);
  g_key_file_set_boolean (cfg, "Common", "Tray", data.tray);
  g_key_file_set_boolean (cfg, "Common", "HideOnStartup", data.tray_hide);

  /* store scripts */
  store = GTK_TREE_MODEL (gtk_builder_get_object (data.xml, "scripts_store"));
  if (gtk_tree_model_get_iter_first (store, &iter))
    {
      guint num = 0;

      do
	{
	  gchar *name, *cmd;
	  gchar *name_k, *cmd_k;
      
	  gtk_tree_model_get (store, &iter, 0, &name, 1, &cmd, -1);

	  name_k = g_strdup_printf ("Name%d", num);
	  cmd_k = g_strdup_printf ("Script%d", num);

	  g_key_file_set_string (cfg, "Scripts", name_k, name);
	  g_key_file_set_string (cfg, "Scripts", cmd_k, cmd);

	  g_free (name_k);
	  g_free (cmd_k);
	  num++;
	}
      while (gtk_tree_model_iter_next (store, &iter));
    }

  /* store printer settings */
  if (data.print != NULL)
    gtk_print_settings_to_key_file (data.print, cfg, NULL);
  if (data.page != NULL)
    gtk_page_setup_to_key_file (data.page, cfg, NULL);

  cfg_file = g_build_filename (g_get_user_config_dir (), "gshotrc", NULL);
  g_mkdir_with_parents (g_get_user_config_dir (), 0755);
  g_file_set_contents (cfg_file, g_key_file_to_data (cfg, NULL, NULL), -1, NULL);
  g_free (cfg_file);

  g_key_file_free (cfg);
  return 0;
}
